﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DAL;
namespace BAL
{
    public class BAL_PHONG
    {
        private static QuanLyTiemNetDataContext db = new QuanLyTiemNetDataContext();
        //thêm
        public static bool ThemPhong(ref string err,string MaPhong,string TenPhong,float GiaThueMay)
        {
            bool them = false;
            try
            {
                PHONG phong = new PHONG();
                phong.MaPhong = MaPhong;
                phong.TenPhong = TenPhong;
                phong.GiaThueMay = GiaThueMay;
                phong.DaXoa = false;
                db.PHONGs.InsertOnSubmit(phong);
                db.SubmitChanges();
                them = true;

            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return them;
        }
        // cập nhật
        public static  bool CapNhatPhong(ref string err,string MaPhong,string TenPhong,float GiaThueMay)
        {
            bool update = false;
            try
            {
                PHONG tim = db.PHONGs.Single(p => p.MaPhong.Equals(MaPhong));
                if(tim!=null)
                {
                    tim.TenPhong = TenPhong;
                    tim.GiaThueMay = GiaThueMay;
                    db.SubmitChanges();
                    update = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return update;
        }
        //xóa
        public static bool XoaPhong(ref string err,string MaPhong)
        {
            bool xoa = false;
            try
            {
                PHONG tim = db.PHONGs.Single(p => p.MaPhong.Equals(MaPhong));
                if(tim!=null)
                {
                    db.PHONGs.DeleteOnSubmit(tim);
                    db.SubmitChanges();
                    xoa = true;

                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return xoa;
        }
        //đánh dấu xóa
        public static bool DanhDauXoa(ref string err, string MaPhong,bool value)
        {
            bool danhdauxoa = false;
            try
            {
                PHONG tim = db.PHONGs.Single(p => p.MaPhong.Equals(MaPhong));
                if(tim!=null)
                {
                    tim.DaXoa = value;
                    db.SubmitChanges();
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return danhdauxoa;
        }
        //ConvertToDatatTable
        public static DataTable ConvertToDataTable(List<PHONG> list)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MaPhong");
            dt.Columns.Add("TenPhong");
            dt.Columns.Add("GiaThueMay");
            //dt.Columns.Add("DaXoa");
            foreach(var i in list)
            {
                dt.Rows.Add(i.MaPhong, i.TenPhong, i.GiaThueMay);
            }
            return dt;
        }
        //lấy thông tin
        public static DataTable LayThongTinPhong()
        {
            var phong = from p in db.PHONGs where p.DaXoa==false select p;
            return ConvertToDataTable(phong.ToList());
        }
        //lấy lịch sử đã xóa phòng
        public static DataTable LayLichSuPhong()
        {
            var phong = from p in db.PHONGs where p.DaXoa == true select p;
            return ConvertToDataTable(phong.ToList());
        }
    
    }
}
